<template>
    <view
        v-if="show"
        class="u-loading"
        :class="mode === 'circle' ? 'u-loading-circle' : 'u-loading-flower'"
        :style="[cricleStyle]"
    >
    </view>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { LoadingProps } from './types';

defineOptions({
    name: 'u-loading'
});

/**
 * loading 加载动画
 * @description 此组件为一个小动画，目前用在uView的loadmore加载更多和switch开关等组件的正在加载状态场景。
 * @tutorial https://uviewpro.cn/zh/components/loading.html
 * @property {String} mode 模式选择，见官网说明（默认circle）
 * @property {String} color 动画活动区域的颜色，只对 mode = flower 模式有效（默认#c7c7c7）
 * @property {String|Number} size 加载图标的大小，单位rpx（默认34）
 * @property {Boolean} show 是否显示动画（默认true）
 * @example <u-loading mode="circle"></u-loading>
 */
const props = defineProps(LoadingProps);

/**
 * 加载中圆圈动画的样式
 * @returns {Record<string, any>}
 */
const cricleStyle = computed(() => {
    let style: Record<string, any> = {};
    style.width = props.size + 'rpx';
    style.height = props.size + 'rpx';
    // 只对圆圈模式生效
    if (props.mode === 'circle') {
        style.borderColor = `#e4e4e4 #e4e4e4 #e4e4e4 ${props.color ? props.color : '#c7c7c7'}`;
    }
    return style;
});
</script>

<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';

.u-loading-circle {
    /* #ifndef APP-NVUE */
    display: inline-flex;
    /* #endif */
    vertical-align: middle;
    width: 28rpx;
    height: 28rpx;
    background: 0 0;
    border-radius: 50%;
    border: 2px solid;
    border-color: #e5e5e5 #e5e5e5 #e5e5e5 #8f8d8e;
    animation: u-circle 1s linear infinite;
}

.u-loading-flower {
    width: 20px;
    height: 20px;
    display: inline-block;
    vertical-align: middle;
    -webkit-animation: u-flower 1s steps(12) infinite;
    animation: u-flower 1s steps(12) infinite;
    background: transparent
        url()
        no-repeat;
    background-size: 100%;
}

@keyframes u-flower {
    0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
    }
    to {
        -webkit-transform: rotate(1turn);
        transform: rotate(1turn);
    }
}

@keyframes u-circle {
    0% {
        transform: rotate(0);
    }
    100% {
        transform: rotate(360deg);
    }
}
</style>
